﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.admin
{
    public partial class MasterAdmin : System.Web.UI.MasterPage
    {
        AccountManagement accountMana = new AccountManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        CompanyBussiness.Account account=new Account();
        CompanyBussiness.AccountLog accountlog;
        VwLogUser accLog;
        VwPermissionUser vw = new VwPermissionUser();
        Modul modul;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (Session[CONST.SESSION_ACCOUNTUSER].ToString() == null)
                {
                    accountMana.LogOut();
                    Response.Redirect("Login.aspx");
                }
                else
                {
                    
                    account = accountMana.GetAccountByUserName(Session[CONST.SESSION_ACCOUNTUSER].ToString());
                    List < VwPermissionUser > list = accountMana.GetPermissionUserByAccountID(account.AccountID);
                    foreach (var t in list)
                    {
                        modul = accountMana.GetModulByID(t.ModulID);
                        if (modul.ModulCode.Contains("MNEWS"))
                        {
                            menuNews.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MSHAREHOLDERS"))
                        {
                            menuSHAREHOLDERS.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MJOB"))
                        {
                            menuJOB.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MHELP"))
                        {
                            menuHelp.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MCONTACT"))
                        {
                            menuContact.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MINTRO"))
                        {
                            menuIntro.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MPRODUCT"))
                        {
                            menuProduct.Visible = true;
                        }

                        if (modul.ModulCode.Contains("MACCOUNT"))
                        {
                            menuAccount.Visible = true;
                        }
                    }

                    btnProfile.Text = Session[CONST.SESSION_ACCOUNTUSER].ToString();
                    if (accountMana.GetLastLog(Session[CONST.SESSION_ACCOUNTUSER].ToString()) != null)
                    {
                        accLog = accountMana.GetLastLog(Session[CONST.SESSION_ACCOUNTUSER].ToString());                       
                    }
                    else
                    {
                        accLog = accountMana.GetLogByUserName(Session[CONST.SESSION_ACCOUNTUSER].ToString());                     
                    }


                    litLastLogin.Text = accLog.AccountLogTime.ToString();
                    
                    
                    
                    account = accountMana.GetAccountByUserName(Session[CONST.SESSION_ACCOUNTUSER].ToString());
                   // avatar.ImageUrl = account.Images.ToString();

                    this.LoadDropLanguage();
                }    
            }
        }

        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            accountMana.LogOut();
            Response.Redirect("Login.aspx");
        }

        protected void btnViewLog_Click(object sender, EventArgs e)
        {

        }

        protected void btnProfile_Click(object sender, EventArgs e)
        {

        }

        protected void LoadDropLanguage()
        {
            dropLanguage.Items.Clear();

            dropLanguage.DataSource = languageMana.GetLanguageList(true);
            dropLanguage.DataBind();

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] != null)
            {
                dropLanguage.SelectedValue = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
            }
        }
          
        protected void btnChangePass_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "ChangePass";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=ChangePass");
            
        }

        protected void btnAdminIndex_Click(object sender, EventArgs e)
        {
            Response.Redirect("Administrator.aspx");
        }

        protected void dropLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[CONST.SESSION_LANGUAGE] = dropLanguage.SelectedValue;
            Response.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID]=dropLanguage.SelectedValue;
            if (Request.Cookies[CONST.ACTION] == null)
            {
                Response.Redirect("Administrator.aspx");
            }
            else
            {
                Response.Redirect("Administrator.aspx?"+CONST.ACTION+"=" + Request.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME]);
            }
        }

        protected void btnViewCategoryNews_Click(object sender, EventArgs e)
        {
            //Danh mục tin tức
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod=" + "MNEWS");
            
        }

        protected void btnAddNewCategory_Click(object sender, EventArgs e)
        {
            //Tạo danh mục tin tức
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewCategory";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddCategory";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewCategory&Mod=" + "MNEWS");
        }

        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            //Đăng tin tức
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&Mod="+ "MNEWS");
        }

        protected void btnViewTypeProduct_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "TypeProduct";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=TypeProduct&Mod=" + "MPRODUCT");
        }

        protected void btnAddNewTypeProduct_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewTypeProduct";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddTypeProduct";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewTypeProduct&Mod=" + "MPRODUCT");
        }

        protected void btnAddNewProduct_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateProduct";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddProduct";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateProduct&Mod=" + "MPRODUCT");
        }

        protected void btnViewCategoryIntro_Click(object sender, EventArgs e)
        {
            //Danh mục giới thiệu
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod=" + "MINTRO");
        }

        protected void btnAddNewCategoryIntro_Click(object sender, EventArgs e)
        {
            //Tạo danh mục giới thiệu
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewCategory";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddCategory";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewCategory&Mod=" + "MINTRO");
        }

        protected void btnViewSHAREHOLDERS_Click(object sender, EventArgs e)
        {
            //Danh mục tin quan hệ cổ đông
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod=" + "MSHAREHOLDERS");
        }

        protected void btnAddNewSHAREHOLDERS_Click(object sender, EventArgs e)
        {
            //Tạo danh mục tin quan hệ cổ đông
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewCategory";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddCategory";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewCategory&Mod=" + "MSHAREHOLDERS");
        }

        protected void btnPostSHAREHOLDERS_Click(object sender, EventArgs e)
        {
            //Đăng tin quan hệ cổ đông
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&Mod=" + "MSHAREHOLDERS");
        }

        protected void btnViewHelp_Click(object sender, EventArgs e)
        {
            //Danh mục các mục trợ giúp người dùng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod=" + "MHELP");
        }

        protected void btnAddNewHelp_Click(object sender, EventArgs e)
        {
            //Tạo danh mục trợ giúp người dùng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewCategory";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddCategory";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewCategory&Mod=" + "MHELP");
        }

        protected void btnPostHelp_Click(object sender, EventArgs e)
        {
            //Đăng bài trợ giúp người dùng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&Mod=" + "MHELP");
        }

        protected void btnViewJob_Click(object sender, EventArgs e)
        {
            //Danh mục tin tuyển dụng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CategoryNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CategoryNews&Mod=" + "MJOB");
        }

        protected void btnPostJob_Click(object sender, EventArgs e)
        {
            //Đăng bài tuyển dụng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&Mod=" + "MJOB");
        }

        protected void btnPostContact_Click(object sender, EventArgs e)
        {
            //Cập nhật nội dung liên hệ
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "CreateNews";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddNews";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&Mod=" + "MCONTACT&CategoryNameContact=" + "MCONTACT");
        }

        protected void btnAccountManagement_Click(object sender, EventArgs e)
        {
            //Quản trị người dùng
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AccountMana";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AccountMana&Mod=" + "MACCOUNT");
        }



    }
}
